package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.BrandResVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author liuyong
 * @email sunlightcs@gmail.com
 * @date 2020-07-06 14:12:56
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取品牌所关联的分类的列表
     *
     * @param brandId
     * @return
     */
    @RequestMapping("/catelog/list")
    public R getBrandRelationListById(@RequestParam("brandId") long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationService
                .list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", categoryBrandRelationEntities);
    }
    @GetMapping("/brands/list")
    public R categoryBrandRelation(@RequestParam(value = "catId",required = true) Long catId) {

       List<BrandEntity> brands =categoryBrandRelationService.getBrandsListByCatId(catId);
        List<BrandResVo> collect = brands.stream().map((e) -> {
            BrandResVo brandResVo = new BrandResVo();
            brandResVo.setBrandId(e.getBrandId());
            brandResVo.setBrandName(e.getName());
            return brandResVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
//    @RequestMapping("/save")
//    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
//		categoryBrandRelationService.save(categoryBrandRelation);
//
//        return R.ok();
//    }

    /**
     *  保存品牌与分类关系的关联关系
     * @param categoryBrandRelationEntity
     * @return
     */
    @PostMapping("/save")
    public R saveCategorybrandrelation(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity) {
        BrandEntity brandEntity = brandService.getById(categoryBrandRelationEntity.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelationEntity.getCatelogId());
        categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        categoryBrandRelationService.save(categoryBrandRelationEntity);
        return R.ok();
    }

        /**
         * 修改
         */
        @RequestMapping("/update")
        public R update (@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
            categoryBrandRelationService.updateById(categoryBrandRelation);

            return R.ok();
        }

        /**
         * 删除
         */
        @RequestMapping("/delete")
        public R delete (@RequestBody Long[]ids){
            categoryBrandRelationService.removeByIds(Arrays.asList(ids));

            return R.ok();
        }

    }
